import { Toast, Dialog } from "vant";
import { scrollMixin } from "../../../utils/mixin";
import cTitle from "components/title";
import ticketGoods from "../consumptionComm/ticketGoods/ticketGoods";
export default {
  components: {
    cTitle,
    ticketGoods
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      keyWord: "",
      tabIndex: 0,
      status: 4,
      timer: "",
      tabList: [
        {
          status: 4,
          index: 0,
          name: "全部",
          goodsList: [],
          page: 1
        },
        {
          status: 0,
          index: 1,
          name: "待付款",
          goodsList: [],
          page: 1
        },
        {
          status: 1,
          index: 2,
          name: "待使用",
          goodsList: [],
          page: 1
        },
        {
          status: 3,
          index: 3,
          name: "已使用",
          goodsList: [],
          page: 1
        },
        {
          status: -1,
          index: 4,
          name: "退款/售后",
          goodsList: [],
          page: 1
        }
      ],
      orderLastPage: 0,
      isLoadMore: true,
      storeApiArr: [
        "plugin.coupon-store.frontend.order-list.index",
        "plugin.coupon-store.frontend.order-list.waitPay",
        "plugin.coupon-store.frontend.order-list.waitSend",
        "plugin.coupon-store.frontend.order-list.completed",
        "plugin.coupon-store.frontend.order-list.refund"
      ]
    };
  },
  created() {
    
    
  },
  activated() {
    if ((this.$route.query.status ?? "") !== "") {
      for (let i in this.tabList) {
        //跳转到对应的状态
        if (this.tabList[i].status === Number(this.$route.query.status)) {
          this.tabIndex = this.tabList[i].index;
        }
      }
    }
    this.tabList[this.tabIndex].page = 1;
    
    this.tabList.forEach((item,index) => {
      item['goodsList'] = [];
    });
    this.storeGoodsList();
  },
  methods: {
    onOrderChange(index) {
      this.tabIndex = index;
      if (this.tabList[this.tabIndex].goodsList.length === 0) {
        this.storeGoodsList();
      }
    },
    orderstateOn(...params) {
      let [value, id, goodsId, refundId, api, storename] = [...params];
      this.getorderState({ value, id, goodsId, refundId, api, storename });
    },
    getorderState(...args) {
      let { value, id, goodsId, refundId, api, storename } = { ...args[0] };
      let query = {
        coupon_store_order_detail: () => {
          this.$router.push(this.fun.getUrl("orderDetails", {}, { orderId: id }));
        }, //订单详情
        coupon_store_refund_page: () => {
          this.$router.push(this.fun.getUrl("refundOrder", {}, { orderId: id, refundType: "2" }));
        }, //申请售后
        1: () => {
          this.$router.push(this.fun.getUrl("orderpay", { order_ids: id, status: "2" }));
        }, //支付
        41: `${api}`, //客服
        coupon_store_comment: () => {
          this.$router.push(this.fun.getUrl("conevaluate", {}, { orderId: id, storeName: storename }));
        }, //评价
        coupon_store_rebuy: () => {
          this.$router.push(this.fun.getUrl("goodsDetails", {}, { goodsId: goodsId }));
        }, //商品详情
        coupon_store_refunding: () => {
          this.$router.push(this.fun.getUrl("refundProcess", {}, { refundId: refundId }));
        }, //售后详情
        coupon_store_refunded: () => {
          this.$router.push(this.fun.getUrl("refundProcess", {}, { refundId: refundId }));
        }
      };
      if (value.toString() === "9" || value.toString() === "12") {
        Dialog.confirm({
          title: "提示",
          message: value === 9 ? "确定取消该订单?" : "确定删除该订单?"
        })
          .then(() => {
            // on close
            value === 9 ? this.cancelPay(id, api) : this.deletePay(id, api);
          })
          .catch(() => {
            // on cancel
          }); //删除订单
      } else if (value.toString() === "41") {
        if (query[value]) {
          window.location.href = query[value];
        } else { 
          this.$toast('请设置客户链接');
        }
      }
      else {
        let paramsObj = query[value];
        return paramsObj.call(this);
      }
    },
    cancelPay(id, api) {
      let json = {
        order_id: id
      };
      $http.post(api, json, "正在获取").then(
        response => {
          if (response.result == 1) {
            Toast(response.msg);
            this.storeGoodsList();
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    deletePay(id, api) {
      let json = {
        order_id: id
      };
      $http.post(api, json, "正在获取").then(
        response => {
          if (response.result == 1) {
            Toast(response.msg);
            this.storeGoodsList();
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    orderSearch(detail) {
      this.keyWord = detail;
      // 防抖
      if (this.timer) clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.debounce(this.storeGoodsList());
      }, 500);
    },
    debounce(fn, delay) {
      let last, deferTimer;
      return function(args) {
        let that = this;
        let _args = arguments;
        let now = +new Date();
        if (last && now < last + delay) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function() {
            last = now;
            fn.apply(that, _args);
          }, delay);
        } else {
          last = now;
          fn.apply(that, _args);
        }
      };
    },
    ondetails(id) {
      this.$router.push(this.fun.getUrl("orderDetails", {}, { orderId: id }));
    },
    handleScroll() {
      //滚动事件触发
      if (this.getScrollTop() + this.getClientHeight() + 105 > this.getScrollHeight()) {
        //此处发起请求
        if (this.isLoadMore) {
          this.getMoreData();
        } else {
          console.log("没有更多数据");
        }
      }
    },
    storeGoodsList() {
      //订单列表
      let api = this.storeApiArr[this.tabIndex];
      let json = {
        page: this.tabList[this.tabIndex].page,
        keyword: this.keyWord.trim()
      };
      $http.post(api, json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.isLoadMore = true;
            this.orderLastPage = res.last_page;
            if (!this.orderLastPage) {
              this.orderLastPage = 0;
            }
            this.tabList[this.tabIndex].goodsList = res.data;
          } else {
            this.storePage = this.storePage - 1;
            this.isLoadMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.tabList[this.tabIndex].page >= this.orderLastPage) {
        return;
      } else {
        this.tabList[this.tabIndex].page += 1;
        let api = this.storeApiArr[this.tabIndex];
        let json = {
          page: this.tabList[this.tabIndex].page,
          keyword: this.keyWord
        };
        $http.post(api, json, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              let list = res.data;
              this.isLoadMore = true;
              let tabIndex = this.tabIndex;
              this.tabList[tabIndex].goodsList = [...this.tabList[tabIndex].goodsList, ...list];
            } else {
              this.tabList[this.tabIndex].page -= 1;
              this.isLoadMore = false;
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    }
  }
};
